/*
 * ChatTextPane.java
 *
 * Created on 26.11.2007, 19:11:07
 *
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.possoft.client.gui.panel.chatpanel.components;

import java.awt.Color;
import java.awt.Font;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

import cz.possoft.client.gui.panel.nastavenispodnipanel.panel.NastaveniShowPanel;

/**
 *
 * @author mara
 *
 * pole kde chodi zpravy od protihrace
 */
public class ChatTextPane extends JTextPane {

    private JScrollPane jsp; // ScrollPane kterej je zavesenej na ten TextPane
    private JScrollBar jsb; // vertikalni scrollbar
    private static Font f = new Font("Times New Roman", 10, 19);

    // styleContext pouzity na styly tohoto JTextPanu
    private StyleContext styleContext;

    // styly
    private Style defaultStyle;
    private Style blueStyle;
    private Style redStyle;


    private Pattern p;
    private Matcher m;
    private String ss;

    public JScrollPane getJsp() {
        return jsp;
    }

    public JScrollBar getJsb() {
        return jsb;
    }

    public ChatTextPane(StyleContext sc) {
        super(new DefaultStyledDocument(sc));
        styleContext = sc;
        initStyles();
        jsp = new JScrollPane();
        jsp.getViewport().add(this);
        jsp.setAutoscrolls(true);
        jsb = jsp.getVerticalScrollBar();
        setFont(f);
    }

    // iniciuje styly
    private void initStyles() {
        defaultStyle = styleContext.getStyle(StyleContext.DEFAULT_STYLE);        
        blueStyle = styleContext.addStyle(null, null);
        StyleConstants.setForeground(blueStyle, Color.BLUE);
        redStyle = styleContext.addStyle(null, null);
        StyleConstants.setForeground(redStyle, Color.RED);
    }

    // vraci defaultStyleDocument
    private DefaultStyledDocument getDSDocument() {
        return (DefaultStyledDocument) getStyledDocument();
    }

    // vlozi cisty retezec s do tohoto textpanu se stylem style
    private void append_text(String s, Style style) {
        try {
            getDSDocument().insertString(getDSDocument().getLength(), s, style);
        } catch (Exception e) {
            System.err.println("VYJIMKA V METODE append_text.");
            e.printStackTrace();
        }
    }

    // vlozi retezec s i se smajliky. Z retezce uz je vyparsovano jmeno hrace.
    private void append_without_player_name(String s) {
        int pp;
        int rr;
        int t = 0;

        p = Pattern.compile("\\*\\d*\\*");
        m = p.matcher(s);
        if (m.find()) {
            // Nalezen smajlik
            pp = m.start();
            rr = m.end();
            ss = s.substring(0, pp);
            try {
                t = Integer.parseInt(s.substring(pp + 1, rr - 1));
            } catch (NumberFormatException nfe) {
                System.err.println("NUMBER FORMAT EXCEPTION V VYSTUP.APPENDER");
                t = 0;
            }
            append_text(ss, blueStyle);
            if (t < SmajlPanel.POCET_SMAJLIKU) {
                // Pred vlozenim ikonky smajlika se musi nastavit oznaceni textu
                setSelectionStart(getStyledDocument().getLength());
                insertIcon(SmajlPanel.getSmajlikButton(t));
                
                // vlozeni prazdneho smajlika - aby sli vlozit dva stejni smajlici za sebe
                setSelectionStart(getStyledDocument().getLength());
                insertIcon(SmajlPanel.getSmajlikButton(SmajlPanel.EMPTY_SMAJLIK));
            }
            append_without_player_name(s.substring(rr, s.length()));
        } else {
            append_text(s, blueStyle);
            setSelectionStart(getStyledDocument().getLength() + 1);
            
            // na konec vkladame prazdneho smajlika
            insertIcon(SmajlPanel.getSmajlikButton(SmajlPanel.EMPTY_SMAJLIK));
            append_text("\r\n", defaultStyle);
        }
    }

    // vlozi retezec se smajliky do tohoto text panu a s modre obarvenym jmenem hrace.
    public void append(String s) {
        // zmodra jmeno hrace
        s = addBluePLayerName(s);
        append_without_player_name(s);
    }
    
    // zpusobi smazani celeho obsahu. Volano pri nacteni nove hry
    public void clearDocument() {
        try {
        getDocument().remove(0, getDocument().getLength());
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String addBluePLayerName(String s) {
        String clientName = NastaveniShowPanel.getConnectThread().getClientName();
        String opponentName = NastaveniShowPanel.getConnectThread().getOpponentName();
        int len;
        if (s.indexOf(clientName) == 0) {
            len = clientName.length();
        } else {
            len = opponentName.length();
        }
        append_text(s.substring(0, len + 1), redStyle);
        return " " + s.substring(len + 2);
    }
}
